package com.qf.hystrixdemo.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.qf.common.vo.ResultVo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HystrixController {


    @HystrixCommand(fallbackMethod = "test1Fallback"
            , commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "70"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "5000")
    }
            , threadPoolKey = "test1-pool"
            , threadPoolProperties = {
            @HystrixProperty(name = "coreSize", value = "3"),
            @HystrixProperty(name = "maxQueueSize", value = "2")
    }

    )
    @GetMapping("/test1")
    public ResultVo test1(Integer num) throws InterruptedException {
        System.out.println(Thread.currentThread().getName() + ":test1 被调用了---->");
        if (num < 10) {
            Thread.sleep(3000);
        }
//        int i = 1/0;
        return new ResultVo(1, "ok", null);
    }

    // test1接口的降级方法
    public ResultVo test1Fallback(Integer num) throws InterruptedException {
        System.out.println("=============>");
        return new ResultVo(-100, "降级", "当前接口无法访问，这降级后的数据" + num);
    }

}
